package com.ksm.zyz.controller;


import com.ksm.zyz.common.base.BaseController;
import com.ksm.zyz.common.base.Global;
import com.ksm.zyz.common.util.DateUtil;
import com.ksm.zyz.common.util.EncodeUtil;
import com.ksm.zyz.sys.entity.ExtData;
import com.ksm.zyz.sys.entity.Message;
import com.ksm.zyz.sys.entity.SysUser;
import com.ksm.zyz.sys.service.SysUserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;


@Controller
@RequestMapping("sysUser")
public class SysUserController extends BaseController<SysUser> {

	@Autowired
	private SysUserService sysUserService;

	/**
	 * 查询全部用户
	 */
	@RequestMapping(value="list")
	@ResponseBody
	public ExtData getList(SysUser sysUse, HttpServletRequest request) {
		sysUse.setBsData(getBsData(request));
		sysUse.setRoleId(getUserInfo().getRoleId());
		extData.setTotalCount(sysUserService.sysselectCount(sysUse));
		extData.setList(sysUserService.selectAll(sysUse));
		return extData;
	}

	/**
	 * 添加用户
	 *
	 * @param sysUser
	 */
	@RequestMapping(value = "create")
	@ResponseBody
	public Message createUser(SysUser sysUser) {
		sysUser.setId(DateUtil.getUUID());
		sysUser.setPassword(EncodeUtil.MD5Encode(sysUser.getPassword()));
		sysUserService.save(sysUser);
		return new Message(Global.SUCCESSFUL);
	}

	/**
	 * 修改用户
	 *
	 * @param sysUser
	 * @return
	 */
	@RequestMapping(value = "update", method = RequestMethod.POST)
	@ResponseBody
	public Message update(SysUser sysUser) {
		sysUserService.update(sysUser);
		return new Message(Global.SUCCESSFUL);
	}

	/**
	 * 删除用户
	 *
	 * @return
	 */
	@RequestMapping(value = "delete")
	@ResponseBody
	public Message delete(String ids) {
		String[] id = ids.split(",");
		sysUserService.delete(id);
		return new Message(Global.SUCCESSFUL);
	}

	/**
	 * Ajax请求校验loginName是否唯一。
	 */
	@RequestMapping(value = "checkLoginName")
	@ResponseBody
	public Message checkLoginName(String loginName,String id) {
		if (id != null && !"".equals(id)) {
			SysUser user = sysUserService.selectById(id);
			if (user != null) {
				if (!user.getLoginName().equals(loginName)) {
					if (sysUserService.findByLoginName(loginName) == null) {
						return new Message(Global.SUCCESSFUL);
					} else {
						return new Message(false,Global.FAILURE);
					}
				}else{
					return new Message(Global.SUCCESSFUL);
				}
			}else{
				return new Message(false,Global.FAILURE);
			}
		}else{
			if (sysUserService.findByLoginName(loginName) == null) {
				return new Message(Global.SUCCESSFUL);
			} else {
				return new Message(false,Global.FAILURE);
			}
		}
	}

	/**
	 * 修改密码
	 * @param req
	 * @return
	 */
	@RequestMapping("changePwd")
	@ResponseBody
	public Object changePwd(HttpServletRequest req){
		SysUser sysUser = getUserInfo();
		String oldPwd = EncodeUtil.MD5Encode(req.getParameter("oldPwd"));
		String userpwd = sysUser.getPassword();
		if(oldPwd == userpwd || oldPwd.equals(userpwd)){
			sysUser.setPassword(EncodeUtil.MD5Encode(req.getParameter("newPwd")));
			sysUserService.update(sysUser);
			return new Message(Global.SUCCESSFUL);
		}else{
			return new Message(false,Global.FAILURE);
		}
	}

	/**
	 * 重置密码
	 * @return
	 */
	@RequestMapping("resetPwd")
	@ResponseBody
	public Object resetPwd(String ids){
		sysUserService.resetPwd(EncodeUtil.MD5Encode("123456"), ids);
		return new Message(Global.SUCCESSFUL);
	}


	/**
	 * 用户注销
	 * @return
	 */
	@RequestMapping("stop")
	@ResponseBody
	public Message stop(String ids){
		String[] userIds = ids.split(",");
		map.put("userIds", userIds);
		map.put("state", "0");
		sysUserService.change(map);
		return new Message(Global.SUCCESSFUL);
	}

	/**
	 * 用户启用
	 * @return
	 */
	@RequestMapping("start")
	@ResponseBody
	public Message start(String ids){
		String[] userIds = ids.split(",");
		map.put("userIds", userIds);
		map.put("state", "1");
		sysUserService.change(map);
		return new Message(Global.SUCCESSFUL);
	}

}
